import os
import subprocess
from types import FunctionType

from anycloud import config
from anycloud.engine.base import Engine
from anycloud.error import NotFoundError

class SerialEngine(Engine):
    _params = (
        config.StringParameter(
            'serial.python', 'Path to python binary',
            default='python'
        ),
    )

    def _run_task(self, task):
        # good for now, uses same task API as other engines
        # but, in the future (when ready to optimize), need to do this without export
        wd = task._export()
        cwd = os.getcwd()
        os.chdir(wd)
        subprocess.call([self.python, "__task__.py"])
        task._process_result(wd)
        task._cleanup()
        os.chdir(cwd)

    def retrieve(self, tasks):
        for task in tasks:
            self._run_task(task)
        return [t.result for t in tasks]



